package softwaretest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import softwaretest.Domain.Device;
import softwaretest.Domain.DeviceData;
import softwaretest.Domain.CameraData;
import softwaretest.repository.DeviceDataRepository;
import softwaretest.repository.DeviceRepository;
import softwaretest.repository.CameraDataRepository;
import softwaretest.dto.DeviceDataRequest;
import softwaretest.dto.ImageRequest;
import softwaretest.dto.GreenhouseRequest;
import softwaretest.dto.DeviceDataDTO; // 引入 DTO
import softwaretest.dto.CameraDataDTO; // 引入 DTO
import softwaretest.dto.DeviceDTO; // 引入 DTO
import softwaretest.dto.AirTemperatureDTO; // 引入空气温度 DTO

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/data")
public class DataController {

    @Autowired
    private DeviceDataRepository deviceDataRepository;

    @Autowired
    private DeviceRepository deviceRepository;

    @Autowired
    private CameraDataRepository cameraDataRepository;

    // 获取土壤PH值的最新10个数据
    @PostMapping("/soil-ph")
    public ResponseEntity<?> getLatestSoilPH(@RequestBody DeviceDataRequest request) {
        List<DeviceData> soilPHData = deviceDataRepository.findTop10ByDevice_CategoryOrderByCreatedAtDesc(request.getCategory());
        if (soilPHData.isEmpty()) {
            return ResponseEntity.status(404).body("No data found for category: " + request.getCategory());
        }

        // 转换为 DTO
        List<DeviceDataDTO> soilPHDataDTO = soilPHData.stream()
                .map(data -> new DeviceDataDTO(data.getCreatedAt(), data.getValue()))
                .collect(Collectors.toList());

        return ResponseEntity.ok(soilPHDataDTO);
    }

    // 获取二氧化碳浓度的最新一个数据
    @PostMapping("/co2-concentration")
    public ResponseEntity<?> getLatestCO2Concentration(@RequestBody DeviceDataRequest request) {
        DeviceData co2Data = deviceDataRepository.findFirstByDevice_CategoryOrderByCreatedAtDesc(request.getCategory());
        if (co2Data == null) {
            return ResponseEntity.status(404).body("No data found for category: " + request.getCategory());
        }

        // 转换为 DTO
        DeviceDataDTO co2DataDTO = new DeviceDataDTO(co2Data.getCreatedAt(), co2Data.getValue());

        return ResponseEntity.ok(co2DataDTO);
    }

    // 获取土壤含水率的最新一个数据
    @PostMapping("/soil-moisture")
    public ResponseEntity<?> getLatestSoilMoisture(@RequestBody DeviceDataRequest request) {
        DeviceData soilMoistureData = deviceDataRepository.findFirstByDevice_CategoryOrderByCreatedAtDesc(request.getCategory());
        if (soilMoistureData == null) {
            return ResponseEntity.status(404).body("No data found for category: " + request.getCategory());
        }

        // 转换为 DTO
        DeviceDataDTO soilMoistureDataDTO = new DeviceDataDTO(soilMoistureData.getCreatedAt(), soilMoistureData.getValue());

        return ResponseEntity.ok(soilMoistureDataDTO);
    }

    // 获取土壤温度的最新10个数据
    @PostMapping("/soil-temperature")
    public ResponseEntity<?> getLatestSoilTemperature(@RequestBody DeviceDataRequest request) {
        List<DeviceData> soilTemperatureData = deviceDataRepository.findTop10ByDevice_CategoryOrderByCreatedAtDesc(request.getCategory());
        if (soilTemperatureData.isEmpty()) {
            return ResponseEntity.status(404).body("No data found for category: " + request.getCategory());
        }

        // 转换为 DTO
        List<DeviceDataDTO> soilTemperatureDataDTO = soilTemperatureData.stream()
                .map(data -> new DeviceDataDTO(data.getCreatedAt(), data.getValue()))
                .collect(Collectors.toList());

        return ResponseEntity.ok(soilTemperatureDataDTO);
    }

    // 获取空气温度的最新10个数据
    @PostMapping("/air-temperature")
    public ResponseEntity<?> getLatestAirTemperature(@RequestBody DeviceDataRequest request) {
        List<DeviceData> airTemperatureData = deviceDataRepository.findTop10ByDevice_CategoryOrderByCreatedAtDesc(request.getCategory());
        if (airTemperatureData.isEmpty()) {
            return ResponseEntity.status(404).body("No data found for category: " + request.getCategory());
        }

        // 转换为 AirTemperatureDTO
        List<AirTemperatureDTO> airTemperatureDataDTO = airTemperatureData.stream()
                .map(data -> new AirTemperatureDTO(data.getCreatedAt(), data.getValue()))
                .collect(Collectors.toList());

        return ResponseEntity.ok(airTemperatureDataDTO);
    }

    // 获取最新的5张图片
    @PostMapping("/images")
    public ResponseEntity<?> getLatestImages(@RequestBody ImageRequest request) {
        List<CameraData> images = cameraDataRepository.findTop5ByOrderByCreatedAtDesc();
        if (images.isEmpty()) {
            return ResponseEntity.status(404).body("No images found");
        }

        // 转换为 DTO
        List<CameraDataDTO> imagesDTO = images.stream()
                .map(image -> new CameraDataDTO(image.getCreatedAt(), image.getPhoto()))
                .collect(Collectors.toList());

        return ResponseEntity.ok(imagesDTO);
    }

    // 获取大棚绑定的设备
    @PostMapping("/devices")
    public ResponseEntity<?> getDevicesByGreenhouse(@RequestBody GreenhouseRequest request) {
        List<Device> devices = deviceRepository.findByGreenhouse_Id(request.getGreenhouseId());
        if (devices.isEmpty()) {
            return ResponseEntity.status(404).body("No devices found for greenhouse ID: " + request.getGreenhouseId());
        }

        // 转换为 DTO
        List<DeviceDTO> devicesDTO = devices.stream()
                .map(device -> new DeviceDTO(device.getId(), device.getName(), device.getCategory()))
                .collect(Collectors.toList());

        return ResponseEntity.ok(devicesDTO);
    }
}